package fr.esisar.cs420.swt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import fr.esisar.cs420.traitement.Application;
import fr.esisar.cs420.traitement.Client;

public class GestionCompte extends Shell {
	private Text mail;
	private Text mdp;
	private Text nom;
	private Text prenom;
	private Text adresse;
	private Text codePostal;
	private Text ville;
	private Text erreurCodePostal;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String args[]) {

	}

	/**
	 * Create the shell.
	 * @param display
	 */
	public GestionCompte(Display display) {
		super(display, SWT.SHELL_TRIM);
		
		Group group = new Group(this, SWT.NONE);
		group.setText("Informations");
		group.setBounds(10, 10, 500, 304);
		
		Label lblMail = new Label(group, SWT.NONE);
		lblMail.setText("Mail  :");
		lblMail.setBounds(23, 26, 55, 15);
		
		mail = new Text(group, SWT.BORDER);
		mail.setEditable(false);
		mail.setBounds(114, 25, 260, 21);
		mail.setText(Application.client.getMail());
		
		Label lblMotDePasse = new Label(group, SWT.NONE);
		lblMotDePasse.setText("Mot de passe  :\r\n");
		lblMotDePasse.setBounds(23, 57, 86, 15);
		
		mdp = new Text(group, SWT.BORDER);
		mdp.setBounds(114, 55, 260, 21);
		mdp.setText(Application.client.getMdp());
		
		Label label_2 = new Label(group, SWT.NONE);
		label_2.setText("Nom :");
		label_2.setBounds(23, 88, 55, 15);
		
		nom = new Text(group, SWT.BORDER);
		nom.setBounds(114, 85, 260, 21);
		nom.setText(Application.client.getNom());
		
		Label label_3 = new Label(group, SWT.NONE);
		label_3.setText("Prenom :");
		label_3.setBounds(23, 123, 55, 15);
		
		prenom = new Text(group, SWT.BORDER);
		prenom.setBounds(114, 120, 260, 21);
		prenom.setText(Application.client.getPrenom());
		
		Label lblAdresse = new Label(group, SWT.NONE);
		lblAdresse.setText("Adresse  :");
		lblAdresse.setBounds(23, 159, 55, 15);
		
		adresse = new Text(group, SWT.BORDER);
		adresse.setBounds(114, 153, 260, 21);
		adresse.setText(Application.client.getAdresse());
		
		Label lblCodePostal = new Label(group, SWT.NONE);
		lblCodePostal.setText("Code postal :");
		lblCodePostal.setBounds(23, 196, 86, 15);
		
		codePostal = new Text(group, SWT.BORDER);
		codePostal.setBounds(114, 190, 260, 21);
		codePostal.setText(String.valueOf((Application.client.getCodePostal())) );
		
		ville = new Text(group, SWT.BORDER);
		ville.setBounds(114, 229, 260, 21);
		ville.setText(Application.client.getVille());
		
		Label lblVille = new Label(group, SWT.NONE);
		lblVille.setText("Ville   :");
		lblVille.setBounds(23, 232, 55, 15);
		
		Button valider = new Button(group, SWT.NONE);
		valider.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				try{
					
					int code = Integer.valueOf(codePostal.getText()) ;
					Client.gererCompte(mail.getText(), mdp.getText(), nom.getText(), prenom.getText(), adresse.getText(), code, ville.getText());						
					Application.client = new Client(mail.getText(), mdp.getText(), nom.getText(), prenom.getText(), adresse.getText(), code, ville.getText());
					fermer();
				}
					catch(NumberFormatException e1 ){
						
						erreurCodePostal.setText("Incorrect");
				}
					
				
			}
		});
		valider.setText("Valider");
		valider.setBounds(334, 269, 75, 25);
		
		Button annuler = new Button(group, SWT.NONE);
		annuler.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				
				fermer();
			}
		});
		annuler.setText("Annuler");
		annuler.setBounds(415, 269, 75, 25);
		
		erreurCodePostal = new Text(group, SWT.NONE);
		erreurCodePostal.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		erreurCodePostal.setBounds(384, 190, 106, 21);
		createContents();
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("Gestion du Compte");
		setSize(527, 354);

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
	
	protected void fermer(){
		
		this.dispose();
		
	}

}
